const mysql = require('mysql2/promise');
const config = require('./database-config.js');

async function fixTalentStoneLevels() {
    const connection = await mysql.createConnection(config.source);

    console.log('🔧 修复天赋石等级数据...');

    try {
        // 删除现有数据
        await connection.execute('DELETE FROM talent_stone_level');
        console.log('✅ 清空现有等级数据');

        // 重新插入完整的等级数据 (1-160级)
        const levels = [];
        for (let level = 1; level <= 160; level++) {
            // 根据文档中的公式计算元气值
            const energy = Math.floor(100 * Math.pow(1.1, level - 1));
            // 银两消耗：每级递增
            const silverCost = level * 1000;

            levels.push([level, energy, silverCost]);
        }

        // 批量插入
        const insertSql = 'INSERT INTO talent_stone_level (level, energy, silver_cost) VALUES ?';
        await connection.query(insertSql, [levels]);
        console.log(`✅ 插入 ${levels.length} 个等级数据`);

        // 验证数据
        const [count] = await connection.execute('SELECT COUNT(*) as count FROM talent_stone_level');
        console.log(`📊 等级数据总数: ${count[0].count}`);

        // 显示前10级数据
        const [first10] = await connection.execute('SELECT * FROM talent_stone_level ORDER BY level LIMIT 10');
        console.log('📋 前10级数据:');
        first10.forEach(level => {
            console.log(`  等级 ${level.level}: 元气=${level.energy}, 银两消耗=${level.silver_cost}`);
        });

        // 显示等级3的数据
        const [level3] = await connection.execute('SELECT * FROM talent_stone_level WHERE level = 3');
        if (level3.length > 0) {
            console.log(`✅ 等级3数据: 元气=${level3[0].energy}, 银两消耗=${level3[0].silver_cost}`);
        } else {
            console.log('❌ 等级3数据不存在');
        }

    } catch (error) {
        console.error('❌ 修复失败:', error.message);
    } finally {
        await connection.end();
    }
}

fixTalentStoneLevels();
